package view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;

public class ErrorWnd extends JFrame{
	
	private		JSplitPane	splitPaneV;
	private		JPanel		panelDesc;
	private		JPanel		panelActions;
	private     JTextArea   txtArea;
	private     JLabel      lableDesc;
	
	//TODO: Move it to main
	private HelpWnd   helpWin;

	public ErrorWnd()
	{
		setTitle( "FW Errors" );
		JPanel topPanel = new JPanel();
		topPanel.setLayout( new BorderLayout() );
		getContentPane().add( topPanel );

		// Create the panels
		createPanelDesc();
		createPaneActions();

		// Create a splitter pane
		splitPaneV = new JSplitPane( JSplitPane.VERTICAL_SPLIT );
		topPanel.add( splitPaneV, BorderLayout.CENTER );
		splitPaneV.setLeftComponent( panelDesc);
		splitPaneV.setRightComponent(panelActions);
	
		pack();
		setLocationRelativeTo(null);
		setEnabled(true);
		setVisible(true);
	}
	
	public void createPanelDesc()
	{
		panelDesc = new JPanel();
		txtArea = new JTextArea();
		txtArea.setEditable(false);
		
		panelDesc.setLayout( new BorderLayout() );
        panelDesc.setPreferredSize( new Dimension( 1000, 500) );
        panelDesc.setMinimumSize( new Dimension( 100, 50 ) );
        
		panelDesc.add( txtArea, BorderLayout.CENTER );
	}
	
	public void createPaneActions()
	{ 
		JButton clearButton = new JButton( "clear" );
		JButton saveButton = new JButton( "Save To File" );
		JButton helpButton = new JButton( "Help" );

		panelActions = new JPanel();
		panelActions.setLayout(new BoxLayout(panelActions, BoxLayout.X_AXIS));
		panelActions.add(clearButton);
		panelActions.add(saveButton);
		panelActions.add(helpButton);
		
		clearButton.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent ae) 
		    {
				Clear();
			}
		}
		);		
		
		saveButton.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent ae) 
		    {
				SaveToFile();
			}
		}
		);		

		helpButton.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent ae) 
		    {
				ShowHelp("Error Window");
			}
		}
		);		
	}
	
	public void AddError(String strErr)
	{
		JOptionPane.showMessageDialog(this, strErr);
		Date now = new Date();
		txtArea.append(SimpleDateFormat.getDateTimeInstance().format(now) +
					" -- "+  strErr + "\n");
	}
	
	private void SaveToFile() 
	{
		JFileChooser chooser = new JFileChooser();
		try
		{
			if(chooser.showSaveDialog(this) ==JFileChooser.APPROVE_OPTION)
			{
				String strFilePath = chooser.getSelectedFile().getPath();
				FileWriter fileOutput = new FileWriter(strFilePath);

				String allText = txtArea.getText() ;
				StringTokenizer st = new StringTokenizer(allText,"\n") ;
				while (st.hasMoreTokens()) 
					fileOutput.write(st.nextToken() + "\r\n");
				
				fileOutput.close();
			}
		}
		catch (IOException ex)
		{
			JOptionPane.showMessageDialog(this, "The action can not be done\n" +
						ex.getMessage());
		}
	}

	private void Clear()
	{
	    String[]  choices = {"YES", "NO"};
	       
		int response = JOptionPane.showOptionDialog(
                null                       // center over parent
              , "This action will clean all errors log.\n Do you still want to perform this action?" // message
              , "clear errors"             // title in titlebar
              , JOptionPane.YES_NO_OPTION  // Option type
              , JOptionPane.PLAIN_MESSAGE  // messageType
              , null                       // icon
              , choices                    // Options
              , "NO"    // initial value
           );
		
		if(response == 0)
		{
			txtArea.setText("");
		}
	}
	
	public void ShowHelp(String hlpTopic)
	{
		if(helpWin == null)
		{
			helpWin = new HelpWnd();
			helpWin.ShowDesc(hlpTopic);
		}					
	}
}
